<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<script type="text/javascript" src="./js/jquery-1.10.2.js"></script>
<script type="text/javascript">

function ins(gwamok_code, curr_inwon, full_inwon, hakjum){
 	var data={"hakbun" : ${userId}, "hakjum" : hakjum}
	
	if(curr_inwon >= full_inwon){ 
		alert('이 과목은 수강인원이 다 찼습니다.');
	}else{
		
		$.ajax({
			type:"post",
			data:data,
			url:"userSumHakjum.do",
			dataType:"json",
			success:function(data1)
			{
				if(data1.result=='impossible'){
					alert('최대 수강 학점 24점을 초과하였습니다. 다시 확인해 주세요.');
				}else{
					var tot = data1.result;
					$("#tot").html(tot);
					
					var data={"hakbun" : ${userId}, "name" : '${userName}', "gwamok_code" : gwamok_code};
					
					$.ajax({
						type:"post",
						data:data,
						url:"userInsertGwamok.do",
						dataType:"json",
						success:function(data2){
							if(data2.result=='exists')
							{
								alert('해당 과목이 이미 존재합니다.');
							}else
							{
								$("#gL").empty();
								var head = "<tr id='thfix'><th>과목코드</th><th>과목</th><th>학점</th><th>전공구분</th>"+
										   "<th>교수</th><th>강의실</th><th>시간</th><th>인원</th><th></th>";
										
								$("#gL").append(head);
								
								$(data2).each(function(index,item){
									gL+="<tr>";
									gL+="<td>" + item.gwamok_code + "</td>";
									gL+="<td>" + item.gwamok_name + "</td>";
									gL+="<td>" + item.hakjum + "</td>";
									gL+="<td>" + item.junkong_gubun + "</td>";
									gL+="<td>" + item.gyosu_name + "</td>";
									gL+="<td>" + item.room_name + "</td>";
									gL+="<td>" + item.sigan + "</td>";
									gL+="<td>" + item.curr_inwon + "/" + item.full_inwon + "</td>";
									gL+='<td><input type=button value="신청" onclick="ins(\''+item.gwamok_code+'\' ,\''+item.curr_inwon+'\' ,\''+item.full_inwon+'\',\''+item.hakjum+'\')"></td>';
									gL+="</tr>";
									
									$("#gL").append(gL);
									gL = '';
																			
									
								});
								
								$.ajax({
									type:"post",
									data:data,
									url:"getuserList.do",
									dataType:"json",
									success:function(data3)
									{
										$("#sG").empty();
										var head = "<tr id='thfix'><th>과목코드</th><th>과목</th><th>학점</th><th>전공구분</th>"+
												   "<th>교수</th><th>강의실</th><th>시간</th><th></th>";
												
										$("#sG").append(head);
										
										$(data3).each(function(index,item){
											sG+="<tr>";
											sG+="<td>" + item.gwamok_code + "</td>";
											sG+="<td>" + item.gwamok_name + "</td>";
											sG+="<td>" + item.hakjum + "</td>";
											sG+="<td>" + item.junkong_gubun + "</td>";
											sG+="<td>" + item.gyosu_name + "</td>";
											sG+="<td>" + item.room_name + "</td>";
											sG+="<td>" + item.sigan + "</td>";
											sG+='<td><input type=button value="삭제" onclick="del(\''+item.user_seq+'\',\''+item.hakjum+'\')"></td>';
											sG+="</tr>";
											
											$("#sG").append(sG);
											sG = '';
										}); 
											
									},
									error:function(e){
									}
								});						
							}
						},
						error:function(e){
						}
					}); 
				}
			},
			error:function(e){
			}
		});	

	}
}

function del(user_seq, hakjum){ 
	var gwa_code = '${gwa_code}';
	var data = {"user_seq" : user_seq, "gwa_code":gwa_code};	
	
	$('#totHidden').val($("#tot").html());
	
	var initTot = ( $('#totHidden').val()- hakjum); 
	$("#tot").html(initTot);
	$("#totHidden").val(initTot);
	
	$.ajax({
		type:"post",
		data:data,
		url:"userDeleteGwamok.do",
		dataType:"json",
		success:function(data1)
		{
			
			$("#sG").empty();
			var head = "<tr id='thfix'><th>과목코드</th><th>과목</th><th>학점</th><th>전공구분</th>"+
					   "<th>교수</th><th>강의실</th><th>시간</th><th></th>";
					
			$("#sG").append(head);
			
			$(data1).each(function(index,item){
				sG+="<tr>";
				sG+="<td>" + item.gwamok_code + "</td>";
				sG+="<td>" + item.gwamok_name + "</td>";
				sG+="<td>" + item.hakjum + "</td>";
				sG+="<td>" + item.junkong_gubun + "</td>";
				sG+="<td>" + item.gyosu_name + "</td>";
				sG+="<td>" + item.room_name + "</td>";
				sG+="<td>" + item.sigan + "</td>";
				sG+='<td><input type=button value="삭제" onclick="del(\''+item.user_seq+'\',\''+item.hakjum+'\')"></td>';
				sG+="</tr>";
				
				$("#sG").append(sG);
				sG = '';
			}); 
			
			$.ajax({
				type:"post",
				data:data,
				url:"getGwamokList.do",
				dataType:"json",
				success:function(data1){
					$("#gL").empty();
					var head = "<tr id='thfix'><th>과목코드</th><th>과목</th><th>학점</th><th>전공구분</th>"+
							   "<th>교수</th><th>강의실</th><th>시간</th><th>인원</th><th></th>";
							
					$("#gL").append(head);
					
					$(data1).each(function(index,item){
						gL+="<tr>";
						gL+="<td>" + item.gwamok_code + "</td>";
						gL+="<td>" + item.gwamok_name + "</td>";
						gL+="<td>" + item.hakjum + "</td>";
						gL+="<td>" + item.junkong_gubun + "</td>";
						gL+="<td>" + item.gyosu_name + "</td>";
						gL+="<td>" + item.room_name + "</td>";
						gL+="<td>" + item.sigan + "</td>";
						gL+="<td>" + item.curr_inwon + "/" + item.full_inwon + "</td>";
						gL+='<td><input type=button value="신청" onclick="ins(\''+item.gwamok_code+'\' ,\''+item.curr_inwon+'\' ,\''+item.full_inwon+'\',\''+item.hakjum+'\')"></td>';
						gL+="</tr>";
						
						$("#gL").append(gL);
						gL = '';
																
						
					});
				},
				error:function(e){
				}
			}); 
		},
		error:function(e){
		}
	});
}
</script>

<div>
<input type="button" value="강의 시간표 보기" onclick="location.href='schedule.do'">
</div>

<h2>과목 리스트</h2>
<div id="gwamokList" class="defTable">
	<table id="gL">
		<tr id="thfix">
			<th>과목코드</th>
			<th>과목</th>
			<th>학점</th>
			<th>전공구분</th>
			<th>교수</th>
			<th>강의실</th>
			<th>시간</th>
			<th>인원</th>
			<th></th>
		</tr>
		<c:forEach var="gwamok" items="${gwamokList}" >
			<tr>
				<td>${gwamok.gwamok_code}</td>
				<td>${gwamok.gwamok_name}</td>
				<td>${gwamok.hakjum}</td>
				<td>${gwamok.junkong_gubun}</td>
				<td>${gwamok.gyosu_name}</td>
				<td>${gwamok.room_name}</td>
				<td>${gwamok.sigan}</td>
				<td>${gwamok.curr_inwon}/${gwamok.full_inwon}</td>
				<td><input type=button value="신청" onclick="ins('${gwamok.gwamok_code}',${gwamok.curr_inwon},${gwamok.full_inwon},${gwamok.hakjum})"></td>
			</tr>
		</c:forEach>
	</table>
</div>

<h2>선택한 과목</h2>
<div id="hakjum">
	<input type="hidden" id="totHidden" value="${initTot }">
	<span id="tot">${initTot }</span>/24(신청학점/총학점)
</div>
<div id="selectGwamok" class="defTable">
	<table id="sG">
		<tr id="thfix">
			<th>과목코드</th>
			<th>과목</th>
			<th>학점</th>
			<th>전공구분</th>
			<th>교수</th>
			<th>강의실</th>
			<th>시간</th>
			<th></th>
		</tr>
		<c:forEach var="select" items="${selectList}" >
			<tr>
				<td>${select.gwamok_code}</td>
				<td>${select.gwamok_name}</td>
				<td><input type="hidden" class="hakjum" name="hakjum" value="${select.hakjum}">${select.hakjum}</td>
				<td>${select.junkong_gubun}</td>
				<td>${select.gyosu_name}</td>
				<td>${select.room_name}</td>
				<td>${select.sigan}</td>
				<td><input type=button value="삭제" onclick="del(${select.user_seq},${select.hakjum})"></td>
			</tr>
		</c:forEach>
	</table>
</div>